package testview.zhen.com.myapplication.hencoder.practice_draw2

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

class Practice12PathEffectView : View {
    internal var paint = Paint(Paint.ANTI_ALIAS_FLAG)
    internal var path = Path()

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    init {
        paint.style = Paint.Style.STROKE

        path.moveTo(50f, 100f)
        path.rLineTo(50f, 100f)
        path.rLineTo(80f, -150f)
        path.rLineTo(100f, 100f)
        path.rLineTo(70f, -120f)
        path.rLineTo(150f, 80f)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // 使用 Paint.setPathEffect() 来设置不同的 PathEffect

        // 第一处：CornerPathEffect  把线的拐角改成曲线有弧度
        var c = CornerPathEffect(20f)
        paint.pathEffect = c
        canvas.drawPath(path, paint)

        canvas.save()
        canvas.translate(500f, 0f)
        // 第二处：DiscretePathEffect  线条进行随机的偏离，让轮廓变得乱七八糟。乱七八糟的方式和程度由参数决定
        var d = DiscretePathEffect(20f,5f)
        paint.pathEffect = null
        paint.pathEffect = d
        canvas.drawPath(path, paint)
        canvas.restore()

        canvas.save()
        canvas.translate(0f, 200f)

        // 第三处：DashPathEffect  第一个参数 intervals 是一个数组，它指定了虚线的格式：数组中元素必须为偶数（最少是 2 个），按照「画线长度、空白长度、画线长度、空白长度」……的顺序排列，例如上面代码中的 20, 5, 10, 5 就表示虚线是按照「画 20 像素、空 5 像素、画 10 像素、空 5 像素」的模式来绘制；第二个参数 phase 是虚线的偏移量。
        var da = DashPathEffect(floatArrayOf(20f,5f,10f,2f) ,6f)
        paint.pathEffect = null
        paint.pathEffect=da
        canvas.drawPath(path, paint)
        canvas.restore()

        canvas.save()
        canvas.translate(500f, 200f)

        //三角形
        var trianglePath = Path()
        trianglePath.lineTo(20f, -30f)
        trianglePath.lineTo(40f, 0f)
        trianglePath.close()
        // 第四处：PathDashPathEffect
        var da1 = PathDashPathEffect(trianglePath,40f,0f,PathDashPathEffect.Style.MORPH)
        paint.style=Paint.Style.STROKE
        paint.pathEffect = da1
        canvas.drawPath(path, paint)
        canvas.restore()

        canvas.save()



        canvas.translate(0f, 400f)
        // 第五处：SumPathEffect

        var sumPathEffect = SumPathEffect(d,da)
        paint.pathEffect = sumPathEffect
        canvas.drawPath(path, paint)
        canvas.restore()

        canvas.save()
        canvas.translate(500f, 400f)
        // 第六处：ComposePathEffect
        var com =ComposePathEffect(da,d)
        paint.pathEffect = com
        canvas.drawPath(path, paint)
        canvas.restore()
    }
}
